package com.senyint.imsdk.packet;

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.xmlpull.v1.XmlPullParser;

public class AttachmentPacketProvider implements PacketExtensionProvider {
	private static final String TAG = AttachmentPacketProvider.class.getSimpleName();

	@Override
    public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
		AttachmentPacketExtension ape = new AttachmentPacketExtension();
		boolean done = false;
		while (!done) {
			int eventType =  parser.next();// 获取事件类型  
			String name = parser.getName();  
			
            switch (eventType) {  
            case XmlPullParser.START_TAG://开始读取某个标签  
                //通过getName判断读到哪个标签，然后通过nextText()获取文本节点值，或通过getAttributeValue(i)获取属性节点值  
                if (name.equalsIgnoreCase("key")) {
                	ape.setKey(parser.nextText());
                } else  if (name.equalsIgnoreCase("name")) {
                	ape.setName(parser.nextText());
                } else  if (name.equalsIgnoreCase("code")) {
                	ape.setCode(parser.nextText());
                } else  if (name.equalsIgnoreCase("type")) {
                	ape.setType(Integer.parseInt(parser.nextText()));
                } else  if (name.equalsIgnoreCase("content")) {
                	ape.setContent(parser.nextText());
//                	ape.setContent(EncodeUtil.hexstring2string(parser.nextText()));
                } else  if (name.equalsIgnoreCase("remark")) {
                	ape.setRemark(parser.nextText());
                }  
                break;  
            case XmlPullParser.END_TAG:// 结束元素事件  
                //读完一个Person，可以将其添加到集合类中  
            	 if (AttachmentPacketExtension.ELEMENT_NAME.equals(name)) {
                     done = true;
                 }
                break;  
            }  
        }
        return ape ;
    }
	
    public IQ parseIQ(XmlPullParser parser) throws Exception {
    	AttachmentPacketExtension data = (AttachmentPacketExtension) parseExtension(parser);
        IQ iq = new AttachmentIQ(data);
        return iq;
    }

}
